<?php
class DatatypeTable{
	var $tableName = null;
	
	function DatatypeTable($tableName){
		$this->tableName = $tableName;
	}
	
	function getCreateTableSQL( $dataypeArray ){
		$sqlArray = array();
		
		$sql .= _LF."CREATE TABLE `".$this->tableName."` ("._LF;
		
		$tblSQlArray 				 = array();
		$tblKeysArray 			 = array();
		$tblConstraintsArray = array();
		$dependants 				 = array();
		
		foreach($dataypeArray as $key=>$val){
			$colSql = '`'.$val->getColumn().'` '.$val->getType().' '.$this->getLengthSQL($val).' ';
			$colSql .= ($alter['unsigned'] ? ' UNSIGNED ' : '');
			$colSql .= ($alter['zeroFill'] ? ' ZEROFILL ' : '');
			$colSql .= ($alter['allowNULL'] ? ' NULL ' : ' NOT NULL ');
			$colSql .= $this->getDefaultSQL($val);
			$colSql .= ($val->getIsAutoInc() ? ' AUTO_INCREMENT ' : '');
			
			$tblSQlArray[] = $colSql ;
			
			$relationship  = $val->getRelationship();
			
			if( $relationship ){
				//TODO:: Need to do error checking on column matching 
				$dependantOn[] = array('table'=>$relationship['table'], 'column'=>$val->getColumn(), 'type'=> $val->getType(), 'length'=> $val->getLength() );//Which table needs to be created first so does not throw error 150
				$fkName = 'fk_'.$this->tableName.'_'.$val->getColumn().'__'.$relationship['table'].'_'.$relationship['column'];
				$tblConstraintsArray[] = 'CONSTRAINT `'.$fkName.'` FOREIGN KEY (`'.$val->getColumn().'`) REFERENCES `'.$relationship['table'].'` (`'.$relationship['column'].'`) ON DELETE CASCADE ON UPDATE NO ACTION';
			}
			
			
			if( $val->getIsAutoInc() ){
				$tblKeysArray[] = 'PRIMARY KEY (`'.$val->getColumn().'`)';
			}else if( $val->getIsPrimary() ){
				$tblKeysArray[] = 'PRIMARY KEY (`'.$val->getColumn().'`)';
			}else if( $val->getIsUnique() ){
				$tblKeysArray[] = 'UNIQUE KEY `'.$val->getColumn().'` (`'.$val->getColumn().'`)';
			}else if( $val->getIsIndex() ){
				$tblKeysArray[] = ' KEY `'.$val->getColumn().'` (`'.$val->getColumn().'`)';
			}
		}
		
		
		if( $tblKeysArray ){
			$tblSQlArray[] =  implode(','._LF, array_unique($tblKeysArray));
		}
		
		if( $tblConstraintsArray ){
			$tblSQlArray[] =  implode(','._LF, array_unique($tblConstraintsArray));
		}
		
		
		$sql .= implode(','._LF, $tblSQlArray)._LF;
		$sql .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		
		$sqlArray[] = $sql;
		
		return array('sql'=>$sqlArray, 'dependantOn'=>$dependantOn);
	}
	function getAlterSQL($datatype){
		$sqlArray = array();
		
		$alter = array();
		$alter['name'] 			= $datatype->getColumn();
		$alter['default'] 	= $datatype->getDefault();
		$alter['autoInc'] 	= $datatype->getIsAutoInc();
		$alter['unique'] 		= $datatype->getIsUnique();
		$alter['allowNULL'] = $datatype->getIsAllowNull();
		$alter['zeroFill'] 	= $datatype->getIsZeroFill();
		$alter['unsigned']	= $datatype->getIsUnsigned();
		
		
		switch( $datatype->getType() ){
			case 'int':{
				break;
			}
		}
		
		
		if( $datatype->getIsAutoInc() ){
			$alter['unsigned']	= true;
			$alter['allowNULL'] = false;
		}
		
		
		$sql = "ALTER TABLE `".$this->tableName."` ADD `".$alter['name']."` ".$datatype->getType()." ".$this->getLengthSQL($datatype);
		$sql .= ($alter['unsigned'] ? ' UNSIGNED ' : '');
		$sql .= ($alter['zeroFill'] ? ' ZEROFILL ' : '');
		$sql .= ($alter['allowNULL'] ? ' NULL ' : ' NOT NULL ');
		$sql .= $this->getDefaultSQL($datatype);
		$sql .= ($datatype->getIsAutoInc() ? ' AUTO_INCREMENT  PRIMARY KEY ' : '');
		$sql .= ';'._LF;
		
		$sqlArray[] = $sql;
		
		//Keys
		if( $datatype->getIsAutoInc() ){
			$sqlArray[] = 'ALTER TABLE `'.$this->tableName.'` ADD PRIMARY KEY (`'.$datatype->getColumn().'`);'._LF;
		}else if( $datatype->getIsPrimary() ){
			$sqlArray[] = 'ALTER TABLE `'.$this->tableName.'` ADD PRIMARY KEY (`'.$datatype->getColumn().'`);'._LF;
		}else if( $datatype->getIsUnique() ){
			$sqlArray[] = 'ALTER TABLE `'.$this->tableName.'` ADD UNIQUE INDEX (`'.$datatype->getColumn().'`);'._LF;
		}else if( $datatype->getIsIndex() ){
			$sqlArray[] = 'ALTER TABLE `'.$this->tableName.'` ADD INDEX  (`'.$datatype->getColumn().'`);'._LF;
		}
		
		//Relationships
		$relationship = $datatype->getRelationship();
		if( $relationship ){
			//TODO:: Need to do error checking on column matching 
			$dependantOn[] = array('table'=>$relationship['table'], 'column'=>$datatype->getColumn(), 'type'=> $datatype->getType(), 'length'=> $datatype->getLength() );//Which table needs to be created first so does not throw error 150
			$fkName = 'fk_'.$this->tableName.'_'.$datatype->getColumn().'__'.$relationship['table'].'_'.$relationship['column'];
			$sqlArray[] = 'ALTER TABLE `'.$this->tableName.'` ADD CONSTRAINT `'.$fkName.'` FOREIGN KEY (`'.$datatype->getColumn().'`) REFERENCES `'.$relationship['table'].'` (`'.$relationship['column'].'`) ON DELETE CASCADE ON UPDATE NO ACTION;';
		}
		
		
		
		return array('sql'=>$sqlArray, 'dependantOn'=>$dependantOn);
	}
	function getDropSQL($datatype){
		$sqlArray = array();
		$dependantOn = array();
		
		$relationship  = $datatype->getRelationship();
		if( $relationship ){
			$dependantOn[] = array('table'=>$relationship['table'], 'column'=>$datatype->getColumn(), 'type'=> $datatype->getType(), 'length'=> $datatype->getLength() );//Which table needs to be created first so does not throw error 150
			$fkName = 'fk_'.$this->tableName.'_'.$datatype->getColumn().'__'.$relationship['table'].'_'.$relationship['column'];
			$sqlArray[] = 'ALTER TABLE `'.$this->tableName.'` DROP FOREIGN KEY `'.$fkName.'`;'._LF;
		}
		$sqlArray[] =  'ALTER TABLE `'.$this->tableName.'` DROP `'.$datatype->getColumn().'`;'._LF;
		
		return array('sql'=>$sqlArray, 'dependantOn'=>$dependantOn);
	}
	function getDefaultSQL($datatype){
		$sql = '';
		
		switch( $datatype->getType() ){
			case 'text':{
				$sql = '';
				break;
			}default:{
				if( !$datatype->getIsAutoInc() ){
					if( !is_null( $datatype->getDefault() ) ){
						$sql .= " DEFAULT '".$datatype->getDefault()."' ";
					}elseif( $datatype->getIsAllowNull()){
						$sql .= " DEFAULT NULL ";
					}
				}
			}
		}
		return $sql;
	}
	function getLengthSQL($datatype){
		switch( $datatype->getType() ){
			case 'text':{
				$sql = '';
				break;
			}default:{
				$sql = '('.$datatype->getLength().')';
			}
		}
		return $sql;
		
	}
}

?>
